/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAOImpl;

import DAO.ICriminalDAO;
import Exception.DBConnectionException;
import POJO.Criminal;
import Util.DBUtils;
import java.math.BigDecimal;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class CriminalImpl extends BaseImpl implements ICriminalDAO{

//    @Override
//    public boolean insert(Object obj) {
//        boolean result = false;
//        
//        try{
//            Connection con =  DBUtils.getConnection();
//            
//                String query = "INSERT INTO [dbo].[Student](FirstName,LastName)VALUES(?,?)";
//            
//            PreparedStatement pstmt = con.prepareStatement(query);
//            
//            Criminal classObj = null;
//            if(obj instanceof Criminal  ){
//                 classObj = (Criminal)obj;
//            }
//            if(classObj != null){
//                pstmt.setString(1, classObj.getName());
//                pstmt.setString(2, classObj.getNickName());
//                
//            }
//            else{
//                return false;
//            }
//            
//            //execute
//            int effectedRows =  pstmt.executeUpdate();
//            
//            if(effectedRows > 0){
//                result = true;
//            }
//            else{
//                result = false;
//            }
//            
//        }
//        catch (SQLException ex) {
//        
//        }
//        catch(DBConnectionException dbException){
//            
//        }
//        
//        return result;
//    }

        @Override
    public boolean insert(Object obj) {
        try{
            Connection con =  DBUtils.getConnection();
            
            String query = " SELECT * FROM Production.Location ";
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = stmt.executeQuery(query);

            //Move cursor to the insert-row
            rs.moveToInsertRow();

            //Set values for insert row
            rs.updateString("Name", "Martins");
            rs.updateBigDecimal(3,new BigDecimal(10.00));
            rs.updateDouble(4, new Double(12.40));

            //Commit append new row in resultSet
            rs.insertRow();

           //Move to the previous row
            rs.moveToCurrentRow();

        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean update(Object obj) {
        try{
            Connection con =  DBUtils.getConnection();
            
            String query = "SELECT * FROM Sales.Store WHERE CustomerID LIKE 70";
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = stmt.executeQuery(query);

            //Move to first row for cusor
            rs.first();

            //Update data n-order column in the first row
            rs.updateString("Name", "abc");

            //Commit the row updation
            rs.updateRow();

        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean delete(Object obj) {
        try{
            Connection con =  DBUtils.getConnection();
            
            String query = " SELECT * FROM Student ";
            Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = stmt.executeQuery(query);

            //Move to first row for cusor
            rs.last();

            //Commit the row deletion
            rs.deleteRow();
            
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }

        return true;
    }
    
    @Override
    public Criminal Show(int ID) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
